﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
 
namespace Version3.SharePoint
{

    [CLSCompliant(false)]
    [Guid("60B2F111-1002-4a1d-A08F-5471A0668206")]
    public class FreezeDriedDataFieldControl : BaseFieldControl  
    {
        protected Label _Label = null;
        private FreezeDriedDataField _ParentField = null;

        public FreezeDriedDataFieldControl(FreezeDriedDataField parentField)
        {
            _ParentField = parentField;
        }

        protected override string DefaultTemplateName
        {
            get
            {
                return "FreezeDriedDataField";
            }
        }

        protected override string DefaultAlternateTemplateName
        {
            get
            {
                return string.Empty;
            }
        }

        protected override void CreateChildControls()
        {
            if (null != this.Field)
            {
                base.CreateChildControls();

                _Label = (Label)TemplateContainer.FindControl("labelFreezeDriedDataField");
                if (null == _Label)
                {
                    _Label = new Label();
                    this.Controls.Add(_Label);
                }
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            if (SPControlMode.Display == this.ControlMode)
            {
                if (this.ItemFieldValue is FreezeDriedDataFieldValue)
                {
                    switch (_ParentField.FormRenderMode)
                    {
                        case FreezeDriedDataField.DATA_TYPE:
                            _Label.Text = SPEncode.HtmlEncode( ((FreezeDriedDataFieldValue)this.ItemFieldValue).DisplayText );
                            break;
                        case FreezeDriedDataField.FIXED_TEXT:
                            _Label.Text = SPEncode.HtmlEncode( _ParentField.FormRenderControlString );
                            break;
                        case FreezeDriedDataField.RENDER_OBJECT:
                            {
                                try
                                {
                                    //Get the class that was defined, and get the FreezeDriedDataFieldRenderer 
                                    //base class and call the hydrate function, then call the render function 
                                    //Version3.SharePoint.SampleFieldRenderer,Version3.FreezeDriedSiteColumn, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d055e77f5fe6a467
                                    int posComma = _ParentField.FormRenderControlString.IndexOf(',');
                                    string className = _ParentField.FormRenderControlString.Substring(0, posComma);
                                    string assemblyPath = _ParentField.FormRenderControlString.Substring(posComma + 1);
                                    Assembly renderAssembly = Assembly.Load(assemblyPath);
                                    FreezeDriedDataFieldRenderer renderingClass = renderAssembly.CreateInstance(className) as FreezeDriedDataFieldRenderer;
                                    renderingClass.HydrateThis(((FreezeDriedDataFieldValue)this.ItemFieldValue).FreezeDriedData);
                                    Panel renderedDiv = new Panel();
                                    renderingClass.Render(renderedDiv);
                                    _Label.Controls.Add(renderedDiv);

                                }
                                catch (Exception renderError)
                                {
                                    Table errorTable = new Table();
                                    errorTable.CssClass = "ms-Error";
                                    errorTable.CellPadding = 0;
                                    errorTable.CellSpacing = 0;
                                    errorTable.BorderWidth = 0;
                                    // first draw the header 
                                    TableRow errorRow = new TableRow();
                                    errorTable.Controls.Add(errorRow);
                                    TableCell errorCell = new TableCell();
                                    errorRow.Controls.Add(errorCell);
                                    errorCell.Text = "This freeze dried object was not re-hydrated";

                                    errorRow = new TableRow();
                                    errorTable.Controls.Add(errorRow);
                                    errorCell = new TableCell();
                                    errorRow.Controls.Add(errorCell);
                                    errorCell.Wrap = true;
                                    errorCell.Text = renderError.Message;
                                    _Label.Controls.Add(errorTable);
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    _Label.RenderControl(output);
                }
            }
            else
            {
                base.Render(output);
            }
        }
    }
}


